//base58加密解密
function Base58() {
	var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
	var ALPHABET_MAP = {};
	var BASE = 58;
	for (var i = 0; i < ALPHABET.length; i++) {
		ALPHABET_MAP[ALPHABET.charAt(i)] = i;
	}

	const ToUTF8 = function(str) {
		var result = new Array();
		var k = 0;
		for (var i = 0; i < str.length; i++) {
			var j = encodeURI(str[i]);
			if (j.length == 1) {
				result[k++] = j.charCodeAt(0);
			} else {
				var bytes = j.split("%");
				for (var l = 1; l < bytes.length; l++) {
					result[k++] = parseInt("0x" + bytes[l]);
				}
			}
		}

		return result;
	}

	const ToUTF16 = function(str) {
		var result = new Array();

		var k = 0;
		for (var i = 0; i < str.length; i++) {
			var j = str[i].charCodeAt(0);
			result[k++] = j & 0xFF;
			result[k++] = j >> 8;
		}

		return result;
	}

	this.encode = function(buffer) {
		var buffer = ToUTF8(buffer)
		if (buffer.length === 0) return '';
		var i,
			j,
			digits = [0];
		for (i = 0; i < buffer.length; i++) {
			for (j = 0; j < digits.length; j++) {
				digits[j] <<= 8;
			}
			digits[0] += buffer[i];
			var carry = 0;
			for (j = 0; j < digits.length; ++j) {
				digits[j] += carry;
				carry = (digits[j] / BASE) | 0;
				digits[j] %= BASE;
			}
			while (carry) {
				digits.push(carry % BASE);
				carry = (carry / BASE) | 0;
			}
		}
		for (i = 0; buffer[i] === 0 && i < buffer.length - 1; i++) digits.push(0);
		return digits
			.reverse()
			.map(function(digit) {
				return ALPHABET[digit];
			})
			.join('');
	}

	const byteToString = function(arr) {
		if (typeof arr === 'string') {
			return arr;
		}
		var str = '',
			_arr = arr;
		for (var i = 0; i < _arr.length; i++) {
			var one = _arr[i].toString(2),
				v = one.match(/^1+?(?=0)/);
			if (v && one.length == 8) {
				var bytesLength = v[0].length;
				var store = _arr[i].toString(2)
					.slice(7 - bytesLength);
				for (var st = 1; st < bytesLength; st++) {
					store += _arr[st + i].toString(2)
						.slice(2);
				}
				str += String.fromCharCode(parseInt(store, 2));
				i += bytesLength - 1;
			} else {
				str += String.fromCharCode(_arr[i]);
			}
		}
		return str;
	}

	this.decode = function(string) {
		if (string.length === 0) return [];
		var i,
			j,
			bytes = [0];
		for (i = 0; i < string.length; i++) {
			var c = string[i];
			if (!(c in ALPHABET_MAP)) throw new Error('Non-base58 character');
			for (j = 0; j < bytes.length; j++) bytes[j] *= BASE;
			bytes[0] += ALPHABET_MAP[c];
			var carry = 0;
			for (j = 0; j < bytes.length; ++j) {
				bytes[j] += carry;
				carry = bytes[j] >> 8;
				bytes[j] &= 0xff;
			}
			while (carry) {
				bytes.push(carry & 0xff);
				carry >>= 8;
			}
		}
		for (i = 0; string[i] === '1' && i < string.length - 1; i++) bytes.push(0);
		return byteToString(bytes.reverse());
	}

	this.stringToHex = function(str) {
		var val ="";
		for (let index = 0; index < str.length; index++) {
			if (val == ""){
				val = str.charCodeAt(index).toString(16);
			}else{
				val += "," + str.charCodeAt(index).toString(16);
			}
			
		}
		return val;
	}

	this.hexToString = function(str) {
		var val="";
		var arr = str.split(",");
		for (let index = 0; index < arr.length; index++) {
			val += String.fromCharCode(parseInt(arr[index],16));
		}
		return val;
	}
}
